package com.dsm.alilinkvisualappdemo

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.core.view.isVisible
import com.dsm.alilinkvisual.biz.AliVideoMainActivity
import com.dsm.alilinkvisualappdemo.databinding.ActivityMainBinding
import com.dsm.alilinkvisualsdk.biz.AliIOTSDKBizByUserAccount
import com.dwy.glide.GlideUtils
import com.dwy.lifecycle.activity.BaseActivity
import com.dwy.permission.PermissionManager
import io.reactivex.android.schedulers.AndroidSchedulers

class MainActivity: BaseActivity() {

    //用户token，从德施曼云同步接口中获取，阿里sdk登录鉴权时需要
    private var userToken = "qPh6soS9C2S11FkUuV2Jv1IQ4559B125"

    //锁ID，从德施曼云同步接口中获取，上报视频接通事件需要
    private var lockId = "2023120713155700340"

    //用户ID，从德施曼云同步接口中获取
    private var userId = "102193430444118016"

    //阿里物联网平台业务ID，从德施曼云同步接口中获取
    private var iotId = "vKGJPs3JD2qlRFMeohpG000000"

    //门铃消息ID，从德施曼云门铃消息中获取
    private var doorBellMsgId = "2342342"

    private lateinit var _binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(_binding.root)

        _binding.apply {
            btnLogin.setOnClickListener {
                /*addSubscription(
                    AliIOTSDKBizByUserAccount.instance.login(userToken, "MainActicity", true)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe({
                            btnVideoTest.isVisible = true
                            Log.d("MainActicity", "login success")
                        }) {
                            btnVideoTest.isVisible = false
                            Log.e("MainActicity", "login fail, error: ${it.stackTraceToString()}")
                        }
                )*/
                GlideUtils.loadImage(R.mipmap.ic_launcher, _binding.ivHead)
            }

            btnVideoTest.setOnClickListener {
                //源码引用更新token
                //LinkVisualSDKManager.updateHttpToken(userToken)

                //lib引用更新token
                AppHttpManager.updateHttpToken(userToken)

                addSubscription(
                    PermissionManager.requestMicroPhonePermission(this@MainActivity)
                        .subscribe({
                            startActivity(Intent(this@MainActivity, AliVideoMainActivity::class.java).apply {
                                putExtra("videoFrom", 0)//业务场景 0 实时视频 1 门铃呼叫 手动配置
                                putExtra("lockId", lockId)
                                putExtra("userId", userId)
                                putExtra("iotId", iotId)
                                putExtra("doorBellMsgId", doorBellMsgId)
                                putExtra("enableBellCallVibrate", true)//是否启用门铃呼叫时手机振动，仅门铃呼叫场景有效 手动配置
                                putExtra("enableWifiSignal", false)//视频页面是否显示wifi信号强度UI 手动配置
                                putExtra("enableChangeVoice", false)//视频页面是否显示变声UI 手动配置
                            })
                        }) {
                            Log.e("MainActivity", "fail, error: ${it.stackTraceToString()}")
                        }
                )
            }
        }
    }
}